package com.qzdsoft.erpcloud.controller.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qzdsoft.erpcloud.domain.basic.InoutStorage;
//import com.qzd.erpcloud.domain.help.RedisHelper;
import com.qzdsoft.erpcloud.domain.po.DataTablePo;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.basic.InoutStorageService;
import com.qzdsoft.erpcloud.vo.query.basic.InoutStorageQueryParam;
//import com.qzdsoft.utils.SessionUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.basic.BasicInoutStorageTypeEnum;
import com.qzdsoft.vo.basic.BasicPreinstallEnum;

/**
 * Created by Admin
 */
@Controller
@RequestMapping("/basic/inout/storage")
public class InoutStorageController {

//    @Autowired
//    private RedisHelper redisHelper;
    
    @Autowired
    CenterCompanyService actCenterCompanyService;

    @Autowired
    private InoutStorageService basicInoutStorageService;

    /**
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        List<EnumPo> typeList = new ArrayList<EnumPo>();
        Map<Object, Object> typeMap = new HashMap<Object, Object>();
        for(BasicInoutStorageTypeEnum e : BasicInoutStorageTypeEnum.values()){
            SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
        }

        model.addAttribute("preinstallList", preinstallList);
        model.addAttribute("preinstallMap", preinstallMap);

        model.addAttribute("typeList", typeList);
        model.addAttribute("typeMap", typeMap);

        model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
        return "basic/inout_storage";
    }

    /**
     * 获取编辑数据
     * @param requestt
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/edit")
    public Object edit(HttpServletRequest requestt, @RequestParam("id") Integer id) {
        if(null != id){
//            return Response.getSuccess(basicInoutStorageService.selectById(id));
            return new Response(ResultEnum.SUCCESS,basicInoutStorageService.selectById(id));
        }
        return Response.error("param missing...");
    }

    /**
     *
     * @param basicInoutStorage
     * @return
     */
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Object editExecute(HttpServletRequest request, InoutStorage basicInoutStorage) {
//        basicInoutStorage.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicInoutStorage.getCompany_id()));
        return basicInoutStorageService.saveOrUpd(basicInoutStorage);
    }

    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return basicInoutStorageService.del(id);
    }

    /**
     *查询
     * @param request
     * @param param
     * @return
     */
    @ResponseBody
    @RequestMapping("/data")
    public Object data(HttpServletRequest request, InoutStorageQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
//        if(null != companyId){
//            param.setCompanyId(companyId);
//        }

        Integer count = basicInoutStorageService.selectDataCount(param.getName(), param.getDecode(), param.getCompanyId(), param.getIsPreinstall(), param.getType());
        List list = basicInoutStorageService.selectData(param.getName(), param.getDecode(), param.getCompanyId(), param.getIsPreinstall(), param.getType(), param.getiDisplayStart(), param.getiDisplayLength());
        return new DataTablePo(count, list, param.getsEcho());
    }
}
